<?php
    include ("../lib/BLL/DmMonhocBLL.php");
    include ("../lib/BLL/DmLoaiMonhocBLL.php");
    include ("../lib/BLL/ChuongTrinhDaoTaoBLL.php");
    include ("../lib/BLL/MonCTDTBLL.php");
    
    $loaimh_bll=new LoaiMonhocBLL();
    $monhoc_bll=new DmMonHocBLL();
    $ctdt_bll=new ChuongTrinhDaoTaoBLL();
    $monctdt_bll=new MonCTDTBLL();
    $monctdt_dal=new MonCTDT();
    $ma_ctdt=(isset($_GET['ma_ctdt']))?$_GET['ma_ctdt']:1;
    $tt="disabled=''";
    $ds_monhoc=$monhoc_bll->getListDmMonHoc();
    $ds_loaimh=$loaimh_bll->getListLoaiMonHoc();
    $ds_ctdt=$ctdt_bll->getAllCTDT();
    $ds_ctdt_ma=$monctdt_bll->getListMonCTDT($ma_ctdt);
    $ds_chuadk=$monhoc_bll->getMonCDK($ma_ctdt);
    if (isset($_POST['btnSua'])){
        if (isset($_POST['checkbox'])){
                $sua=$_POST['masua'];
                foreach($sua as $ma_mon_ctdt){
                $ma_loaimonhoc=$_POST['loaimonhoc'];
                $khoiluong=$_POST['khoiluong'];
                $donvitinh=$_POST['donvitinh'];
                $kq=$monctdt_bll->updateMonCTDT($ma_mon_ctdt,$ma_loaimonhoc,$khoiluong,$donvitinh);
                }
                if ($kq==true){
                    header("location: chuongtrinhdaotao-monctdt-ma_ctdt-".$ma_ctdt);
                }
        }
    }
    if (isset($_POST['btnLuu'])){
        for($i=0; $i<count($ds_chuadk); $i++){
            $donvihoc=$_POST['slbDvh'.$i]; 
            $khoiluong=$_POST['khoiluong'.$i];  
            $ma_loaimonhoc=$_POST['slbLoaimh'.$i]; 
            $ma_monhoc=$_POST['ma_monhoc'.$i];
            $do_save=0;
            if($donvihoc=="-1"){
                $do_save=-1;
            }
            if($khoiluong==""){
                $do_save=-1;
            }
            if($ma_loaimonhoc=="-1"){
                $do_save=-1;
            }
            if($do_save!=-1){
                $monctdt_dal->setMonCTDTDAL($donvihoc,$khoiluong,$ma_ctdt,$ma_loaimonhoc,$ma_monhoc);
                $kq=$monctdt_bll->insertMonCTDT($monctdt_dal);
                if($kq){
                    header("location: chuongtrinhdaotao-monctdt-ma_ctdt-".$ma_ctdt);
                }
            }
        }
    }
    if(isset($_POST['xoachon'])){
        if(isset($_POST['checkall'])){
            $kq=$monctdt_bll->xoaMaCTDT($ma_ctdt);
            if ($kq == true){
                header("location: chuongtrinhdaotao-monctdt-ma_ctdt-".$ma_ctdt);
            }
        }
        if(isset($_POST['checkbox'])){
            $delete=$_POST['checkbox'];
            foreach($delete as $ma_monhoc){
                $kq=$monctdt_bll->xoaMonCTDT($ma_monhoc,$ma_ctdt);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-monctdt-ma_ctdt-".$ma_ctdt);
                }
            }
        }
    }
    
    $smarty->assign('ma_ctdt',$ma_ctdt);
    $smarty->assign('ds_loaimh',$ds_loaimh);
    $smarty->assign('ds_monhoc',$ds_monhoc);
    $smarty->assign('ds_ctdt',$ds_ctdt);
    $smarty->assign('ds_ctdt_ma',$ds_ctdt_ma);
    $smarty->assign('ds_chuadk',$ds_chuadk);
    $smarty->assign('tt',$tt);
    $smarty->display('chuongtrinhdaotao/monCTDT.tpl');
?>